package com.datastructure.linkedlist.self;

import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;

public class DoubleDirectionLinkedList {

    public static void main(String[] args) {
        DoubleDirectionList doubleDirectionLinkedList = new DoubleDirectionList();
        Compartment compartment9 = new Compartment(9,"复兴号第9节车厢","复兴号第9节车厢");
        Compartment compartment3 = new Compartment(3,"复兴号第3节车厢","复兴号第3节车厢");
        Compartment compartment5 = new Compartment(5,"复兴号第5节车厢","复兴号第5节车厢");
        doubleDirectionLinkedList.addCompartmentByOrder(compartment9);
        doubleDirectionLinkedList.addCompartmentByOrder(compartment3);
        doubleDirectionLinkedList.addCompartmentByOrder(compartment5);
        doubleDirectionLinkedList.list();
        Compartment buffer = reverseDoubleDirectionList(doubleDirectionLinkedList.head);
        doubleDirectionLinkedList.head=buffer;
        doubleDirectionLinkedList.list();
    }

    public static int getLength(Compartment head){
        if(head.next==null){
            System.out.println("数据为空");
            return 0;
        }
        int length=0;
        Compartment buffer = head.next;
        while (buffer != null) {
            length++;
            buffer = buffer.next;
        }
        return length;
    }

    public static Compartment getCompartmentByPosition(int no,Compartment head){
        int length = getLength(head);
        Compartment buffer = head.next;
        if(no<0||no>length||buffer==null){
            System.out.println("没有这个位置的数据");
            return null;
        } else {
            for(int i=1;i<length-no;i++){
                buffer=buffer.next;
            }
        }
        return buffer;
    }

    public static Compartment reverseDoubleDirectionList(Compartment head){
        Compartment buffer = head.next;
        Compartment next=null;
        Compartment reverseDoubleDirectionList=new Compartment(1,"和谐号","和谐号动车组列车");
        if(buffer==null){
            System.out.println("不需要翻转数据");
        }
        while (buffer!=null){
            next = buffer.next;
            buffer.next=reverseDoubleDirectionList.next;
            if(reverseDoubleDirectionList.next!=null){
                reverseDoubleDirectionList.next.pre=buffer;
            }
//            reverseDoubleDirectionList.next.pre=buffer;
            reverseDoubleDirectionList.next=buffer;
            buffer.pre=reverseDoubleDirectionList;
            buffer=next;
        }
        return reverseDoubleDirectionList;
    }
}

class DoubleDirectionList {
    public Compartment head = new Compartment(0,"复兴号列车","复兴号全列电动列车");

    public Compartment getCompartment() {
        return head;
    }

    public void addCompartment(Compartment compartment){
        Compartment buff = head.next;
        if(buff==null){
            head.next=compartment;
            compartment.pre=head;
        }else {
            while (buff.next != null) {
                buff = buff.next;
            }
            buff.next=compartment;
            compartment.pre=buff;
        }
    }

    public void list(){
        Compartment buff = head.next;
        if(buff == null){
            System.out.println("暂无新的数据");
        } else {
            while(buff!=null){
                System.out.println(buff);
                buff = buff.next;
            }
        }
    }

    public void updateCompartment(Compartment compartment){
        Compartment buff = head.next;
        boolean flag = false;
        if(buff==null){
           System.out.println("没有可更新数据");
        } else {
            while(buff!=null){
                if(buff.no== compartment.no){
                    flag =true;
                    buff.name=compartment.name;
                    buff.nickName=compartment.nickName;
                }
                buff = buff.next;
            }
        }
        if(!flag){
            System.out.println("链表中没有该数据");
        }
    }

    public void deleteCompartment(int no){
        Compartment buff = head.next;
        if(buff==null){
            System.out.println("没有可删除的数据");
        } else {
            while(buff!=null){
                if(buff.no==no){
                    if(buff.next!=null){
                        buff.pre.next=buff.next;
                        buff.next.pre=buff.pre;
                    } else {
                        buff.pre.next=null;
                    }
                }
                buff = buff.next;
            }
        }
    }

    public void addCompartmentByOrder(Compartment compartment){
        Compartment buff = head.next;
        if(buff==null){
            head.next=compartment;
            compartment.pre=head;
        }else {
            while(true){
                if(compartment.no<buff.no){
                    compartment.next=buff;
                    compartment.pre=buff.pre;
                    buff.pre.next=compartment;
                    buff.pre=compartment;
                    break;
                } else if(compartment.no==buff.no){
                    System.out.println("该数据已经存在!");
                }else if(buff.next==null){
                    buff.next=compartment;
                    compartment.pre=buff;
                }
                buff = buff.next;
            }
        }
    }


}

class Compartment {

    public int no;
    public String name;
    public String nickName;
    public Compartment pre;
    public Compartment next;

    public Compartment(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "Compartment{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
